<?php
namespace app\controller\back;

use app\common\controller\_AdminController;
use app\model\Image;
use think\Validate;

class ImageController extends _AdminController
{	
	protected $rule = [
		'ids|编号集合'			=> 'array',
		'id|编号'				=> 'integer',
		'file|图片'				=> 'file|image|fileExt:jpg,png,jpeg,gif|fileSize:1024000',
		'belong_type|归属类型'	=> 'in:1',
		'start|起始时间'			=> 'dateFormat:Y-m-d',
        'end|结束时间'			=> 'dateFormat:Y-m-d',
		'page|页码'         		=> 'integer',
        'size|条数'        		=> 'integer',
	];
	
	protected $scene = [
		'upload'			=> ['file','belong_type'],
		'update'			=> ['id','belong_type','belong_id'],
        'delete'            => ['ids'],
        'list'              => ['belong_type','start','end','page','size'],
	];

	protected function initialize()
	{	
		
		parent::initialize();
		switch ($this->action) {
			case 'upload':
            	$this->data['file'] = request()->file('file');
                $this->appendRule('file','require');
                break;
            case 'update':
                $this->appendRule('id','require|checkImageById');
                break;
            case 'delete':
                $this->appendRule('ids','require|checkImageById');
                break;
            case 'list':
                
                break;
			default:
				# code...
				break;
		}
		$this->check();
	}

	/**
	 * @api {POST} /back/image/upload 上传图片
	 * @apiVersion 0.0.1
	 * @apiGroup Back-image
	 *
	 * @apiParam {Object} file 上传文件
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "image": "",
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": {
     *         "src": "20190609\\53a20a35ddd7980ec6351258775f9658.jpg"
     *     }
	 * }
	 */
	public function uploadAction()
	{  
		$file = $this->data['file'];
		$upload = $file->move(config('params.images_path'));
		if($upload){

			$data['path'] = str_replace("\\", '/', $upload->getSaveName());
			$data['belong_type'] = !empty($this->data['belong_type'])?$this->data['belong_type']:'';
			$data['user_id'] = $this->user->id;
			$data['user_type'] = $this->user->type;
			Image::doSave($data);
			$res['src'] = request()->domain().'/'.config('params.images_path').str_replace("\\", '/', $upload->getSaveName()); 
			// $res['title'] = $upload->getFilename(); 
		}else{
        	parent::resError($upload->getError());
    	}
        return parent::resSuccess($res,0);
	}


	/**
	 * @api {POST} /back/image/delete 删除图片
	 * @apiVersion 0.0.1
	 * @apiGroup Back-image
	 *
	 * @apiParam {Array[]} ids 编号集合
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "ids[0]": "1",
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function deleteAction()
	{   
		Image::doDelete($this->data['ids']);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/image/list 图片列表
	 * @apiVersion 0.0.1
	 * @apiGroup Back-image
	 *
	 * @apiParam {Number} [belong_type] 图片归属
	 * 
	 * @apiParamExample {json} Request-Example:
     * {
     *     "belong_type[0]": "1",
     * }
     * 
	 * @apiSuccess {Number} [belong_type] 图片归属
	 * @apiSuccess {String} name 名称
	 * @apiSuccess {Number} sort 排序
	 * @apiSuccess {String} desc 描述
	 * @apiSuccess {String} value 值
	 * @apiSuccess {String} type 类型
	 * @apiSuccess {String} create_time 创建时间
	 * @apiSuccess {String} update_time 更新时间
	 * @apiSuccess {String} count 总条数
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": [{
     *         "id": 39,
	 *         "alias": "zcx",
	 *         "name": "ad222",
	 *         "sort": "1",
	 *         "desc": "asds",
	 *         "value": "1",
     *         "type": "number",  
     *         "create_time": "2019-04-09 17:24:19",
     *         "update_time": "2019-04-09 17:24:19",
	 *     }],
	 *     "count": 6
	 * }
	 */
	public function listAction()
	{   
		$list = Config::doList([],$this->page,$this->size);
		return parent::resSuccess($list);
	}
	
	

	// 自定义规则
	protected function checkImageById()
	{
		Validate::extend('checkImageById', function ($value) {
			$image = Image::get($value);
			if(!$image){
				return '记录不存在';
			}
			return true;
		});
	}
}
